package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.ClassInfo;
import cn.stylefeng.guns.modular.school.entity.ClassTeacherLession;
import cn.stylefeng.guns.modular.school.mapper.ClassTeacherLessionMapper;
import cn.stylefeng.guns.modular.school.model.params.ClassTeacherLessionParam;
import cn.stylefeng.guns.modular.school.model.params.TeacherInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ClassTeacherLessionResult;
import cn.stylefeng.guns.modular.school.model.result.TeacherInfoResult;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.ClassTeacherLessionService;
import cn.stylefeng.guns.modular.school.service.TeacherInfoService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 班级教师课程关系 服务实现类
 * </p>
 *
 * @author
 * @since 2019-12-01
 */
@Service
public class ClassTeacherLessionServiceImpl extends ServiceImpl<ClassTeacherLessionMapper, ClassTeacherLession> implements ClassTeacherLessionService {
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private ClassInfoService classInfoService;

    @Override
    public void add(ClassTeacherLessionParam param) {
        ClassTeacherLession entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ClassTeacherLessionParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ClassTeacherLessionParam param) {
        ClassTeacherLession oldEntity = getOldEntity(param);
        ClassTeacherLession newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ClassTeacherLessionResult findBySpec(ClassTeacherLessionParam param) {
        QueryWrapper<ClassTeacherLession> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", param.getClassId()).eq("teacher_id", param.getTeacherId());
        if (ToolUtil.isNotEmpty(param.getLessionId())) {
            queryWrapper.eq("lession_id", param.getLessionId());
        }
        ClassTeacherLession classTeacherLession = this.baseMapper.selectOne(queryWrapper);
        if (classTeacherLession == null) {
            return null;
        }
        ClassTeacherLessionResult classTeacherLessionResult = new ClassTeacherLessionResult();
        ToolUtil.copyProperties(classTeacherLession, classTeacherLessionResult);
        return classTeacherLessionResult;
    }

    @Override
    public List<ClassTeacherLessionResult> findListBySpec(ClassTeacherLessionParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ClassTeacherLessionParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ClassTeacherLessionResult> findCurrentUserClass() {
        TeacherInfoParam param = new TeacherInfoParam();

        if (ShiroKit.hasRole("administrator")) {
            List<ClassTeacherLessionResult> classTeacherLessionResults = new ArrayList<>();
            List<ClassInfo> classInfos = classInfoService.list();
            classInfos.forEach(classInfo -> {
                ClassTeacherLessionResult classTeacherLessionResult = new ClassTeacherLessionResult();
                classTeacherLessionResult.setClassId(classInfo.getId());
                classTeacherLessionResult.setClassName(classInfo.getName());
                classTeacherLessionResults.add(classTeacherLessionResult);
            });

            return classTeacherLessionResults;
        }

        param.setCode(ShiroKit.getUser().getAccount());
        TeacherInfoResult teacherInfoResult = teacherInfoService.findBySpec(param);
        if (teacherInfoResult != null) {
            ClassTeacherLessionParam classTeacherLessionParam = new ClassTeacherLessionParam();
            classTeacherLessionParam.setTeacherId(teacherInfoResult.getId());
            List<ClassTeacherLessionResult> classTeacherLessionResults = this.findListBySpec(classTeacherLessionParam);
            return classTeacherLessionResults;
        }
        return null;
    }

    @Override
    public List<ClassTeacherLession> findByTeacherId(ClassTeacherLessionParam param) {
        return this.list(new QueryWrapper<ClassTeacherLession>().eq("teacher_id", param.getTeacherId()));
    }

    private Serializable getKey(ClassTeacherLessionParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ClassTeacherLession getOldEntity(ClassTeacherLessionParam param) {
        return this.getById(getKey(param));
    }

    private ClassTeacherLession getEntity(ClassTeacherLessionParam param) {
        ClassTeacherLession entity = new ClassTeacherLession();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
